package com.yaorange.teaching.service.impl;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.yaorange.common.core.enums.DailyTaskEnums;
import com.yaorange.common.core.utils.DateUtils;
import com.yaorange.common.security.utils.SecurityUtils;
import com.yaorange.teaching.bo.DailyTaskSubmitCountBo;
import com.yaorange.teaching.domain.YaStudent;
import com.yaorange.teaching.domain.YaTeachingPlan;
import com.yaorange.teaching.mapper.YaStudentMapper;
import com.yaorange.teaching.mapper.YaTeachingPlanMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yaorange.teaching.mapper.YaDailyTasksMapper;
import com.yaorange.teaching.domain.YaDailyTasks;
import com.yaorange.teaching.service.IYaDailyTasksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 每日任务Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-22
 */
@Service
public class YaDailyTasksServiceImpl extends ServiceImpl<YaDailyTasksMapper,YaDailyTasks> implements IYaDailyTasksService
{
    @Autowired
    private YaDailyTasksMapper yaDailyTasksMapper;
    @Autowired
    private YaTeachingPlanMapper yaTeachingPlanMapper;
    @Autowired
    private YaStudentMapper yaStudentMapper;
    /**
     * 查询每日任务
     *
     * @param id 每日任务主键
     * @return 每日任务
     */
    @Override
    public YaDailyTasks selectYaDailyTasksById(Long id)
    {
        return yaDailyTasksMapper.selectYaDailyTasksById(id);
    }

    /**
     * 查询每日任务列表
     *
     * @param yaDailyTasks 每日任务
     * @return 每日任务
     */
    @Override
    public List<YaDailyTasks> selectYaDailyTasksList(YaDailyTasks yaDailyTasks)
    {
        //设置查询条件：只查当前学员自己的数据
        yaDailyTasks.setStudentId(SecurityUtils.getUserId());
        return yaDailyTasksMapper.selectYaDailyTasksList(yaDailyTasks);
    }

    /**
     * 新增每日任务
     *
     * @param yaDailyTasks 每日任务
     * @return 结果
     */
    @Override
    public int insertYaDailyTasks(YaDailyTasks yaDailyTasks)
    {
        yaDailyTasks.setCreateTime(DateUtils.getNowDate());
        return yaDailyTasksMapper.insertYaDailyTasks(yaDailyTasks);
    }

    /**
     * 修改每日任务
     *
     * @param yaDailyTasks 每日任务
     * @return 结果
     */
    @Override
    public int updateYaDailyTasks(YaDailyTasks yaDailyTasks)
    {
        yaDailyTasks.setUpdateTime(DateUtils.getNowDate());
        return yaDailyTasksMapper.updateYaDailyTasks(yaDailyTasks);
    }

    /**
     * 批量删除每日任务
     *
     * @param ids 需要删除的每日任务主键
     * @return 结果
     */
    @Override
    public int deleteYaDailyTasksByIds(Long[] ids)
    {
        return yaDailyTasksMapper.deleteYaDailyTasksByIds(ids);
    }

    /**
     * 删除每日任务信息
     *
     * @param id 每日任务主键
     * @return 结果
     */
    @Override
    public int deleteYaDailyTasksById(Long id)
    {
        return yaDailyTasksMapper.deleteYaDailyTasksById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void autoCreateDailyTasks() {
        //查询到今日所有班级课表
        LambdaQueryWrapper<YaTeachingPlan> queryWrapper = new LambdaQueryWrapper<>();
        //今日
        Date nowDate = DateUtils.getNowDate();
        String date = DateUtils.getDate();
        queryWrapper.eq(YaTeachingPlan::getDate, date);
        //day不能空
        queryWrapper.isNotNull(YaTeachingPlan::getDay);
        List<YaTeachingPlan> teachingPlanList = yaTeachingPlanMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(teachingPlanList)) {
            return;
        }
        List<YaDailyTasks> dailyTasksList = new ArrayList<>(100);
        //为每个班每个学员创建每日任务
        teachingPlanList.forEach(tp -> {
            Integer stage = tp.getStage();
            Long classesId = tp.getClassesId();
            List<YaStudent> studentList = yaStudentMapper.selectYaStudentByClassesId(classesId);
            studentList.forEach(student -> {
                 //判断该任务是否已创建
                LambdaQueryWrapper<YaDailyTasks> dailyTaskQueryWrapper = new LambdaQueryWrapper<>();
                Long id = student.getId();
                dailyTaskQueryWrapper.eq(YaDailyTasks::getStudentId, id);
                dailyTaskQueryWrapper.eq(YaDailyTasks::getDate, date);
                Integer count = yaDailyTasksMapper.selectCount(dailyTaskQueryWrapper);
                if (!count.equals(0)) {
                    return;
                }
                //没有创建，创建
                YaDailyTasks dailyTasks = new YaDailyTasks();
                dailyTasks.setDate(date);
                dailyTasks.setStage(stage);
                dailyTasks.setClassesId(classesId);
                dailyTasks.setStatus(DailyTaskEnums.NOT_START.getCode());
                dailyTasks.setCourseContent(tp.getCourseContent());
                dailyTasks.setStudentId(id);
                dailyTasks.setCreateBy("timer");
                dailyTasks.setCreateTime(nowDate);
                dailyTasksList.add(dailyTasks);
            });
        });
        super.saveBatch(dailyTasksList,100);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void checkDailyTask() {
        LocalDate yesterday = LocalDate.now().plusDays(-1);
        //将昨天提交的&已完成的设置为合格
        yaDailyTasksMapper.updateYaDailyTasksCompleted(yesterday.toString());
        //将昨天未提交的设置为已过期&不合格
        yaDailyTasksMapper.updateYaDailyTasksExpired(yesterday.toString());
    }

    @Override
    @Transactional(readOnly = true)
    public List<YaDailyTasks> queryInterviewList(String date, Long classesId) {
        return yaDailyTasksMapper.selectInterviewList(date,classesId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<DailyTaskSubmitCountBo> querySubmitCount() {
        Long userId = SecurityUtils.getUserId();
        YaStudent yaStudent = yaStudentMapper.selectYaStudentById(userId);
        Long classesId = yaStudent.getClassesId();

        String yesterday = LocalDate.now().plusDays(-1).toString();
        return yaDailyTasksMapper.selectSubmitCount(classesId,yesterday);
    }
}
